home *** CD-ROM | disk | FTP | other *** search
/ MacHack 1997 / MacHack 1997.toast / Hacks / Hacks ’96 / Ahhhhhh Hack / AhhInit.c < prev    next >
C/C++ Source or Header  |  1996-06-22  |  2KB  |  94 lines

  1. /* #include files */
  2.  
  3. #include "A4Stuff.h"
  4. #include "SetupA4.h"
  5. #include "AhhGlobals.h"
  6.  
  7. /* #defines */
  8.  
  9. #define kINITid            0
  10.  
  11. #define SEGMENTED        FALSE        /* define if we have a multiple segment INIT */
  12. #define kNumSegments    2            /* total number of segments, including INIT resource itself */
  13.  
  14. /* some globals local to this file */
  15.  
  16. /* SystemTask patch stuff */
  17.  
  18. typedef pascal void (*ReleaseResourceProc)(Handle releaseMe);
  19. ReleaseResourceProc    gOldReleaseResourceAddr;
  20. pascal void ReleaseResourcePatch(Handle releaseMe);
  21.  
  22. /* main */
  23.  
  24. void main(void)
  25. {
  26.     long    oldA4;
  27.     Handle    h;
  28.     
  29.     #ifdef USE_DEBUGGER_CALLS
  30.         Debugger();
  31.     #endif
  32.  
  33.     /* set up our A4 context for _this file_ */
  34.     oldA4 = SetCurrentA4();
  35.     RememberA4();
  36.     
  37.     /* detach ourselves */
  38.     h = Get1Resource('INIT', kINITid);
  39.     if (h) DetachResource(h);
  40.         
  41.     /* initialize the globals in _this file_ */
  42.     gOldReleaseResourceAddr = 0L;
  43.     
  44.     /* initialize the globals in the _other file_ */
  45.     /* this also forces the code in the other segment to be loaded */
  46.     /* we must force all other segments to be loaded at this point */
  47.     /* so we can detach them below */
  48.     SetMeUp();
  49.     
  50.     /* now that we are sure that all segments have been loaded */
  51.     /* (since we called at least one routine in each segment) */
  52.     /* we can continue by detaching each one */
  53. #if SEGMENTED    
  54.     {
  55.         short i;
  56.         for (i=kINITid+1;i<kNumSegments;++i) {
  57.             h = Get1Resource('INIc', i);    /* should NOT fail since each segment should already be loaded and locked */
  58.             if (h) DetachResource(h);
  59.         }
  60.     }
  61. #endif
  62.  
  63.     /* patch SystemTask */
  64.     gOldReleaseResourceAddr = (ReleaseResourceProc)GetToolTrapAddress(_ReleaseResource);
  65.     SetToolTrapAddress((UniversalProcPtr)ReleaseResourcePatch, _ReleaseResource);
  66.     
  67.     /* restore the a4 world */
  68.     SetA4(oldA4);
  69. }
  70.  
  71. /* SystemTaskPatch */
  72.  
  73. pascal void ReleaseResourcePatch(Handle releaseMe)
  74. {
  75.     long oldA4;
  76.     
  77.     #ifdef USE_DEBUGGER_CALLS
  78.         Debugger();
  79.     #endif
  80.     
  81.     /* use the global data in _this file_ */
  82.     oldA4 = SetUpA4();
  83.     
  84.     /* test the globals in the _other file_ */
  85.     PlayMySound();
  86.     
  87.     /* call through to the original SystemTask */
  88.     gOldReleaseResourceAddr(releaseMe);
  89.     
  90.     /* restore the a4 world */
  91.     RestoreA4(oldA4);
  92. }
  93.  
  94.